package com.hongdee.base.car.controller;

import com.hongdee.base.car.entity.TBcCar;
import com.hongdee.base.car.service.ICarService;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/13.
 */
@RestController
@RequestMapping("/base/car")
public class CarSeriesController {
    @Autowired
    private ICarService carService;


    /**
     * @param uid         登录用户id
     * @param token       登录用户 token
     * @param bCode       品牌编码
     * @param carModule   车辆型号名称
     * @param carModuleNo 车辆型号代码
     * @param type        类型 0车系1车型
     * @return Map
     * @throws Exception
     * @desc 分页查询
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "bCode", defaultValue = "") String bCode,
                    @RequestParam(name = "carModule", defaultValue = "") String carModule,
                    @RequestParam(name = "carModuleNo", defaultValue = "") String carModuleNo,
                    @RequestParam(name = "type", defaultValue = "") String type,
                    @RequestParam(name = "carSeriesCode", defaultValue = "") String carSeriesCode,
                    @RequestParam(name = "allSeries", defaultValue = "") String allSeries
                    ) throws Exception {
        Map map = carService.page(start, limit, bCode, carModule, carModuleNo, type, carSeriesCode, allSeries);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 加厂牌号的树
     * @param uid
     * @param token
     * @param allSeries
     * @param showLevel
     * @param allFlag
     * @return
     * @throws Exception
     */
    @RequestMapping("/pages")
    @ResponseBody
    public List pages(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "allSeries", defaultValue = "") String allSeries,
                     @RequestParam(name = "showLevel", defaultValue = "2") int showLevel,
                     @RequestParam(name = "allFlag", defaultValue = "false") boolean allFlag
    ) throws Exception {

        List treeList = this.carService.pages(uid, showLevel, allSeries,allFlag);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询车系车型成功");
        return treeList;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 车型车系明细
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = this.carService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 启用start
     * @author 徐钦勇
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.carService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 停用stop
     * @author 徐钦勇
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.carService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * @return List<Map>
     * @throws Exception
     * @desc 车系下拉列表
     * @author 徐钦勇
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "companyId",defaultValue = "")String companyId) throws Exception {
        if("".equals(companyId)){
            companyId=TokenUtil.getCompanyId(uid);
        }
        //获得车辆信息

        Map<String, String> comInfo;
        if(companyId.equals(TokenUtil.getCompanyId(uid))){
                comInfo    = TokenUtil.getCompany(uid);

        }else{
            comInfo = this.carService.getOrgInfo(companyId);
        }
        if (comInfo == null) {
            throw new Exception("请先登录！");
        }
        if (comInfo.containsKey("oCarBrand")) {
            //System.out.println(comInfo.get("oCarBrand"));
            return this.carService.combo(comInfo.get("oCarBrand"),companyId);
        } else {
            return new ArrayList<>();
        }

    }

    /**
     * @return List<Map>
     * @throws Exception
     * @desc 车系下拉列表
     * @author 徐钦勇
     */
    @RequestMapping("/queryByLevel")
    @ResponseBody
    public List<Map> queryByLevel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "level", defaultValue = "") String level) throws Exception {
        if("".equals(companyId)){
            companyId=TokenUtil.getCompanyId(uid);
        }
        return this.carService.queryByLevel(companyId, level);
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return ResultEntity
     * @throws Exception
     * @desc 车系保存 Save
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcCar data) throws Exception {
        EntityUtils.valid(data);
        TBcCar tBcCar = this.carService.saveOrUpdate(data);
        ResultEntity res = new ResultEntity();
        res.setData(tBcCar);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 车型车系树
     */
    @RequestMapping("/tree")
    @ResponseBody
    public List tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "allSeries", defaultValue = "") String allSeries,
                     @RequestParam(name = "showLevel", defaultValue = "2") int showLevel,
                     @RequestParam(name = "allFlag", defaultValue = "false") boolean allFlag
    ) throws Exception {
        List treeList = this.carService.buildCarLevelTree(uid, showLevel, allSeries,allFlag);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询车系车型成功");
        return treeList;
    }

    /**
     * 树形品牌车系车型配置
     * @param uid
     * @param token
     * @param allSeries
     * @param showLevel
     * @param allFlag
     * @param brand
     * @return
     * @throws Exception
     */
    @RequestMapping("/newTree")
    @ResponseBody
    public List newTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "allSeries", defaultValue = "") String allSeries,
                     @RequestParam(name = "showLevel", defaultValue = "2") int showLevel,
                     @RequestParam(name = "allFlag", defaultValue = "false") boolean allFlag,
                     @RequestParam(name = "brand", defaultValue = "") String brand
    ) throws Exception {
        List treeList = this.carService.buildLevelTree(uid, showLevel, allSeries,allFlag,brand);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询车系车型成功");
        return treeList;
    }


    /**
     * 车型车系树
     */
    @RequestMapping("/tree/list")
    @ResponseBody
    public ResultEntity treeList(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "allSeries", defaultValue = "") String allSeries,
                     @RequestParam(name = "showLevel", defaultValue = "2") int showLevel
    ) throws Exception {
        List treeList = this.carService.buildCarLevelTree(uid, showLevel, allSeries,false);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询车系车型成功");
        return res;
    }
}
